package com.weetop.cfw.message.fragment

import android.content.Context
import android.os.Build
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SizeUtils
import com.gyf.immersionbar.ImmersionBar
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.MessagePresenterImp
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.base.view.MessageView
import kotlinx.android.synthetic.main.fragment_message.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView


/**
 * 消息界面
 */
class MessageFragment: CommonBaseFragment() {

    val messageTitleList = listOf("系统消息","消费信息")

    private val systemMessageFragment:SystemMessageFragment = SystemMessageFragment()
    private val consumptionMessageFragment:ConsumptionMessageFragment = ConsumptionMessageFragment()

    private val messageFragmentList = listOf<CommonBaseFragment>(systemMessageFragment,consumptionMessageFragment)

    override fun getLayoutId() = R.layout.fragment_message

    override fun initView() {

    }

    override fun initData() {
        messageViewPager.offscreenPageLimit = 2
        if(activity != null){
            messageViewPager.adapter = MesagePagerAdapter(activity!!.supportFragmentManager,messageFragmentList)
            initMagicIndicator()
        }
    }

    private fun initMagicIndicator(){
        val commonNavigator = CommonNavigator(activity)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {

            override fun getCount(): Int {
                return messageTitleList.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val colorTransitionPagerTitleView = SimplePagerTitleView(context)
                colorTransitionPagerTitleView.normalColor = ColorUtils.getColor(R.color.colorAccent)
                colorTransitionPagerTitleView.selectedColor = ColorUtils.getColor(R.color.c_1381CC)
                colorTransitionPagerTitleView.text = messageTitleList[index]
                colorTransitionPagerTitleView.textSize = 16F
                colorTransitionPagerTitleView.paint.isFakeBoldText = true
                colorTransitionPagerTitleView.setOnClickListener { messageViewPager.setCurrentItem(index,true) }
                return colorTransitionPagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                indicator.lineHeight = SizeUtils.dp2px(2F).toFloat()
                indicator.roundRadius = SizeUtils.dp2px(1F).toFloat()
                indicator.setColors(ColorUtils.getColor(R.color.c_1381CC))
                return indicator
            }
        }
        messageMagicIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(messageMagicIndicator, messageViewPager)
    }

    override fun lazyLoadData() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            ImmersionBar.with(this).transparentStatusBar().fitsSystemWindows(true,android.R.color.white).statusBarDarkFont(true).init()
        }else{
            ImmersionBar.with(this).transparentStatusBar().fitsSystemWindows(true,android.R.color.black).statusBarDarkFont(false).init()
        }
    }


    private class MesagePagerAdapter(fragmentManager: FragmentManager, var messageFragmentList:List<CommonBaseFragment>):
        FragmentStatePagerAdapter(fragmentManager){

        override fun getItem(position: Int) = messageFragmentList[position]

        override fun getCount() = messageFragmentList.size

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
//            super.destroyItem(container, position, `object`)
        }

    }
}